const express = require('express');
const cors = require('cors');

const app = express();

app.use(cors());
app.use(express.json());

// 健康检查
app.get('/api/health', (req, res) => {
  res.json({ success: true, message: 'API运行正常' });
});

// 登录
app.post('/api/doctors/login', (req, res) => {
  res.json({
    success: true,
    message: '登录成功',
    data: {
      token: 'mock_token_1_' + Date.now(),
      doctor: {
        doctor_id: 1,
        username: 'admin',
        real_name: '系统管理员',
        role: 3,
        department: '信息科',
        title: '系统管理员'
      }
    }
  });
});

// 获取用户信息
app.post('/api/doctors/info', (req, res) => {
  res.json({
    success: true,
    message: '获取用户信息成功',
    data: {
      doctor_id: 1,
      username: 'admin',
      real_name: '系统管理员',
      role: 3,
      department: '信息科',
      title: '系统管理员'
    }
  });
});

// 评分结果查询 - 修复404错误的关键端点
app.post('/api/scoring/result', (req, res) => {
  console.log('收到评分结果查询请求:', req.body);
  const { task_id } = req.body;
  
  if (!task_id) {
    return res.status(400).json({
      success: false,
      message: '任务ID不能为空',
      data: null
    });
  }

  // 模拟返回结果
  res.json({
    success: true,
    message: '获取任务结果成功',
    data: {
      task_id: task_id,
      patient: {
        patient_id: 1,
        patient_code: 'TEST001',
        gender: 1,
        gender_name: '男',
        birthday: '1990-01-01'
      },
      doctor: {
        doctor_id: 1,
        real_name: '系统管理员',
        department: '信息科',
        title: '系统管理员'
      },
      task_info: {
        status: 3,
        status_name: '已完成',
        submit_time: new Date().toISOString(),
        complete_time: new Date().toISOString(),
        image_url: '/uploads/test.jpg',
        clinical_text: '测试临床文本'
      },
      ai_result: {
        score: 5,
        severity: 2,
        severity_name: '轻度',
        confidence: 85,
        model_version: '1.0.0',
        analysis_details: 'AI分析详情'
      }
    }
  });
});

// 评分提交
app.post('/api/scoring/submit', (req, res) => {
  res.json({
    success: true,
    message: '任务已提交，正在进行分析中...',
    data: {
      task_id: Math.floor(Math.random() * 1000),
      status: 2,
      message: 'AI分析正在进行中，请稍后查看结果'
    }
  });
});

// 评分列表
app.post('/api/scoring/list', (req, res) => {
  res.json({
    success: true,
    message: '获取任务列表成功',
    data: {
      items: [],
      pagination: {
        current_page: 1,
        page_size: 10,
        total: 0,
        total_pages: 0
      }
    }
  });
});

const PORT = 4000;
app.listen(PORT, () => {
  console.log(`🚀 简化测试服务器启动成功! 端口: ${PORT}`);
  console.log(`✅ /api/scoring/result 端点已可用`);
  console.log(`🔍 测试端点: http://localhost:${PORT}/api/health`);
});
